package org.karelun.gef.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;
import org.karelun.gef.directeditor.NodeCellEditorLocator;
import org.karelun.gef.directeditor.NodeDirectEditManager;
import org.karelun.gef.figure.ImageNodeFigure;
import org.karelun.gef.model.AbstractConnectionModel;
import org.karelun.gef.model.Node;
import org.karelun.gef.policies.DeleteNodeEditPolicy;
import org.karelun.gef.policies.NodeConnectionEditPolicy;
import org.karelun.gef.policies.NodeDirectEditPolicy;

public abstract class NodePart extends AbstractGraphicalEditPart implements
		PropertyChangeListener, NodeEditPart {

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new DeleteNodeEditPolicy());
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new NodeConnectionEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new NodeDirectEditPolicy());
	}

	protected abstract IFigure createFigure();

	protected void refreshVisuals() {

	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return new ChopboxAnchor(getFigure());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return new ChopboxAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return new ChopboxAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return new ChopboxAnchor(getFigure());
	}

	protected List<AbstractConnectionModel> getModelSourceConnections() {
		Node model = (Node) getModel();
		List<AbstractConnectionModel> sourceConnection = model
				.getModelSourceConnection();
		return sourceConnection;
	}

	protected List<AbstractConnectionModel> getModelTargetConnections() {
		Node model = (Node) getModel();
		List<AbstractConnectionModel> targetConnection = model
				.getModelTargetConnection();
		return targetConnection;
	}

	public void propertyChange(PropertyChangeEvent evt) {

	}

	public void activate() {
		if (isActive())
			return;
		super.activate();
	}

	public void deactivate() {
		if (!isActive())
			return;
		super.deactivate();
	}

	// direct editor the model's content
	public void performRequest(Request req) {
		if (this instanceof RelationPart) {
			return;
		}
		if (req.getType().equals(RequestConstants.REQ_DIRECT_EDIT)) {
			IFigure figure = getFigure();
			if (figure instanceof ImageNodeFigure)
				return;
			NodeCellEditorLocator locator = new NodeCellEditorLocator(figure);
			DirectEditManager manager = new NodeDirectEditManager(this,
					TextCellEditor.class, locator);
			manager.show();
		}
		super.performRequest(req);
	}
}